<?php

namespace app\api\validate;

use think\Db;
use think\Validate;

/**
 * Class AdminValidate
 * @package app\api\validate
 */
class AdminValidate extends Validate
{

    protected $rule = [

        'account' => ['require', 'checkMyAccount'],
        'password' => ['require'],
        'new_password' => ['require'],
        'u_id' => ['regex' => '/^[+]{0,1}(\d+)$/'],
        'status' => ['in' => '0,1'],
        'start_time' => ['checkMyDate'],
        'end_time' => ['checkMyDate'],
        'id' => ['require', 'regex' => '/^[+]{0,1}(\d+)$/'],
        'page' => ['require'],
        'pageNum' => ['require'],
        'pagenum' => ['require'],
        'group_id' => 'require'
    ];

    protected $message = [
        'id' => 'id不能为空',
        'account.require' => '账号不能为空',
        'account.checkMyAccount' => '账号不存在',
        'password' => '密码不能为空',
        'u_id' => '用户id参数错误',
        'status' => '状态参数错误',
        'start_time' => '开始时间参数错误',
        'end_time' => '结束时间参数错误',
        'page' => 'page不能为空',
        'pageNum' => 'pageNum不能为空',
        'pagenum' => 'pagenum不能为空',
        'group_id' => '请选择权限组id'
    ];

    protected function checkMyDate($value)
    {
        if (date('Y-m-d', strtotime($value)) == $value) {
            return true;
        } else {
            return false;
        }
    }

    protected $scene = [
        // 反馈信息列表
        'feedbackList' => ["pageNum" => ['require', 'number'], "u_id" => "number", "start_time", "end_time"],
        //管理员登录
        'login' => ['account', 'password'],
        // 管理员修改密码
        'editPassword' => ['id', 'password', 'new_password', 'group_id'],
        // 添加管理员
        'addAdmin' => ['account' => 'require', 'password', 'group_id'],
        // 解决问题反馈
        'updateFeedback' => ['id' => 'require'],
        // 管理员列表
        'adminList' => ['pagenum'],
        // 编辑管理员
        'eiditAdmin' => ['account' => 'require', 'password', 'group_id']
    ];

    // 账号不存在验证
    protected function checkMyAccount($value)
    {
        $account = Db::name('Admin')
            ->where('account', '=', $value)
            ->value('account');
        return $account ? true : false;
    }
}